<template>
  <el-container class="admin-layout">
    <el-aside :width="sidebarWidth" class="sidebar">
      <div class="sidebar-header">
        <el-icon :size="24"><ShoppingBag /></el-icon>
        <transition name="fade">
          <span v-if="!isCollapse" class="title">管理后台</span>
        </transition>
      </div>
      
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :collapse-transition="false"
        @select="handleMenuSelect"
      >
        <el-menu-item index="/admin/dashboard">
          <el-icon><DataBoard /></el-icon>
          <template #title>数据概览</template>
        </el-menu-item>
        
        <el-menu-item index="/admin/categories">
          <el-icon><Menu /></el-icon>
          <template #title>分类管理</template>
        </el-menu-item>
        
        <el-menu-item index="/admin/products">
          <el-icon><Box /></el-icon>
          <template #title>商品管理</template>
        </el-menu-item>
        
        <el-menu-item index="/admin/stock">
          <el-icon><Goods /></el-icon>
          <template #title>库存管理</template>
        </el-menu-item>
        
        <el-menu-item index="/admin/orders">
          <el-icon><Tickets /></el-icon>
          <template #title>订单管理</template>
        </el-menu-item>
        
        <el-menu-item index="/admin/refunds">
          <el-icon><RefreshLeft /></el-icon>
          <template #title>退货管理</template>
        </el-menu-item>
        
        <el-menu-item index="/admin/employees">
          <el-icon><User /></el-icon>
          <template #title>员工管理</template>
        </el-menu-item>
        
        <el-menu-item index="/admin/finance">
          <el-icon><TrendCharts /></el-icon>
          <template #title>财务统计</template>
        </el-menu-item>
      </el-menu>
      
      <div class="sidebar-footer">
        <el-button
          :icon="isCollapse ? Expand : Fold"
          circle
          @click="toggleCollapse"
        />
      </div>
    </el-aside>
    
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <span class="page-title">{{ pageTitle }}</span>
        </div>
        
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <div class="user-info">
              <el-avatar :size="32">{{ userStore.userInfo?.username?.charAt(0) }}</el-avatar>
              <span>{{ userStore.userInfo?.username }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item disabled>
                  <el-icon><User /></el-icon> 管理员
                </el-dropdown-item>
                <el-dropdown-item command="logout" divided>
                  <el-icon><SwitchButton /></el-icon> 退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main class="main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessageBox } from 'element-plus'
import { Expand, Fold } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const isCollapse = ref(false)

const sidebarWidth = computed(() => {
  return isCollapse.value ? '64px' : '200px'
})

const activeMenu = computed(() => {
  return route.path
})

const pageTitle = computed(() => {
  return route.meta.title || ''
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleMenuSelect = (index) => {
  router.push(index)
}

const handleCommand = async (command) => {
  if (command === 'logout') {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      type: 'warning'
    })
    await userStore.logout()
    router.push('/login')
  }
}
</script>

<style lang="scss" scoped>
.admin-layout {
  width: 100%;
  height: 100vh;
  
  .sidebar {
    background: #001529;
    transition: width 0.3s;
    
    .sidebar-header {
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 10px;
      color: white;
      font-size: 18px;
      font-weight: bold;
      border-bottom: 1px solid rgba(255, 255, 255, 0.1);
      
      .title {
        white-space: nowrap;
      }
    }
    
    .el-menu {
      border: none;
      background: #001529;
      
      :deep(.el-menu-item) {
        color: rgba(255, 255, 255, 0.65);
        
        &:hover {
          background: #1890ff !important;
          color: white;
        }
        
        &.is-active {
          background: #1890ff !important;
          color: white;
        }
      }
    }
    
    .sidebar-footer {
      position: absolute;
      bottom: 20px;
      left: 50%;
      transform: translateX(-50%);
    }
  }
  
  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    padding: 0 20px;
    
    .header-left {
      .page-title {
        font-size: 18px;
        font-weight: bold;
      }
    }
    
    .header-right {
      .user-info {
        display: flex;
        align-items: center;
        gap: 10px;
        cursor: pointer;
        
        span {
          font-size: 14px;
        }
      }
    }
  }
  
  .main {
    background: #f0f2f5;
    overflow-y: auto;
  }
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>
